package com.dao.impl;

import org.springframework.jdbc.core.JdbcTemplate;
import com.dao.RoleDao;
import com.pojo.FuncBean;

import java.util.*;

@SuppressWarnings(value = { "unchecked" })
public class RoleDaoImpl implements RoleDao
{
	JdbcTemplate jdbcTmplt = null;

	public JdbcTemplate getJdbcTmplt() 
	{
		return jdbcTmplt;
	}

	public void setJdbcTmplt(JdbcTemplate jdbcTmplt) 
	{
		this.jdbcTmplt = jdbcTmplt;
	}
	
	public List getFuncWithRole(long roleId)
	{
		String sql = "select code, name, pcode, num, url, stop from FUNCINFO where stop<>1 order by pcode, num, code";
		List funcs = jdbcTmplt.query(sql, new FuncMapper());
		//System.out.println("funcs size is:"+funcs.size());
		List f1 = new ArrayList();
		List f2 = new ArrayList();
		List f3 = new ArrayList();
		for(int i = 0; i < funcs.size(); i++)
		{
			FuncBean f = (FuncBean) funcs.get(i);
			String pcode = f.getPcode();
			if(pcode == null)
			{
				f1.add(f);
			}else if(pcode.indexOf("-") == -1)
			{
				f2.add(f);
			}else if(pcode.indexOf("-") != -1)
			{
				f3.add(f);
			}
		}
		
		Map m2 = new HashMap();
		Map m3 = new HashMap();		
		
		for(int i= 0; i < f1.size(); i++)
		{
			FuncBean fb = (FuncBean) f1.get(i);
			String code = fb.getCode();
			List tempList = new ArrayList();
			for(int j = 0; j < f2.size(); j++)
			{
				FuncBean fb2 = (FuncBean) f2.get(j);
				String pcode = fb2.getPcode();
				
				if(pcode.equals(code))
				{
					tempList.add(fb2);
				}
			}
						
			m2.put(code, tempList);
		}
		
		for(int i = 0; i < f2.size(); i++)
		{
			FuncBean fb = (FuncBean) f2.get(i);
			String code = fb.getCode();
			List tempList = new ArrayList();
			for(int j = 0; j < f3.size(); j++)
			{
				FuncBean fb3 = (FuncBean) f3.get(j);
				String pcode = fb3.getPcode();
				
				if(pcode.equals(code))
				{
					tempList.add(fb3);
				}
			}
						
			m3.put(code, tempList);			
		}
		
		List rList = new ArrayList();
		rList.add(f1);
		rList.add(m2);
		rList.add(m3);
		
		return rList;
	}
}
